//
//  EFPromotionCell.m
//  EnjogFood
//
//  Created by 王芳 on 17/5/6.
//  Copyright © 2017年 wf. All rights reserved.
//

#import "EFPromotionCell.h"
#import "EFPromotionItem.h"
#import "EFSepecialItem.h"
#import "EFSaleView.h"
#import "EFEventPromotionViewController.h"

@interface EFPromotionCell ()
@property (strong, nonatomic) UIImageView *imageV;
@property (strong, nonatomic) UIScrollView *contentScrollView;
@property (strong, nonatomic) EFPromotionItem *promotionItem;
@end

@implementation EFPromotionCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initUI];
        self.backgroundColor = RGBCOLOR(0xf5f9f9);
    }
    return self;
}

- (void)initUI{
    [self.contentView addSubview:self.imageV];
    [self.contentView addSubview:self.contentScrollView];
    self.contentScrollView.top = self.imageV.bottom;
}

- (void)setObject:(EFPromotionItem *)item{
    self.promotionItem = item;
    [self.imageV setImageWithURL:[NSURL URLWithString:item.img]];
    CGFloat x = 20;
    CGFloat y = 0;
    for (EFSepecialItem *sItem in item.productList) {
        EFSaleView *view = [[EFSaleView alloc] initWithFrame:CGRectMake(x, y, 102, 170) Item:sItem];
        
        x += 102 + 5;
        [self.contentScrollView addSubview:view];
    }
    self.contentScrollView.contentSize = CGSizeMake(20 + item.productList.count * 102 + (item.productList.count - 1)*5, 170);
}

- (UIImageView *)imageV{
    if (!_imageV) {
        _imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, kScreenWidth, 110)];
        _imageV.backgroundColor = RGBCOLOR(0xf3f3f3);
        _imageV.userInteractionEnabled = YES;
        [_imageV addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(turnToPage)]];
        _imageV.contentMode = UIViewContentModeScaleAspectFill;
        _imageV.clipsToBounds = YES;
    }
    return _imageV;
}

- (void)turnToPage{
    EFEventPromotionViewController *vc = [[EFEventPromotionViewController alloc] initWithEventId:self.promotionItem.eventId];
    [ICNavigator pushViewController:vc animated:YES];
}

- (UIScrollView *)contentScrollView{
    if (!_contentScrollView) {
        _contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 170)];
        _contentScrollView.showsHorizontalScrollIndicator = false;
        _contentScrollView.backgroundColor = [UIColor whiteColor];
    }
    return _contentScrollView;
}
@end
